package com.itheima.manager;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {

    ArrayList<Student> list = new ArrayList<>();

    /**
     * 根据学号修改学生
     */
    public void updateStudent() {
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要修改的学号：");
        String id = sc.next();
        System.out.print("请输入新的学生姓名：");
        String name = sc.next();
        System.out.print("请输入新的学生年龄：");
        Integer age = sc.nextInt();
        System.out.print("请输入新的学生地址：");
        String address = sc.next();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                list.get(i).setName(name);
                list.get(i).setAge(age);
                list.get(i).setAddress(address);
            }
        }
        System.out.println("修改成功！");
    }

    /**
     * 根据学号删除学生
     */
    public void deleteStudent() {
        System.out.print("请输入要删除的学号：");

        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                list.remove(i);
            }
        }
        System.out.println("删除成功！");
    }


    /**
     * 查看所有学生
     */
    public void list() {
        if (list.size() == 0) {
            System.out.println("暂无学生数据，请添加后再查询！");
        } else {
            System.out.println("学号 \t姓名 \t年龄 \t地址");
            list.forEach(student -> {
                System.out.println(student.getId() + " \t" + student.getName() + " \t" + student.getAge() + " \t" + student.getAddress());
            });
        }
    }

    /**
     * 添加学生
     */
    public void addStudent() {
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入学号：");
        String id = sc.next();
        System.out.print("请输入姓名：");
        String name = sc.next();
        System.out.print("请输入年龄：");
        int age = sc.nextInt();
        System.out.print("请输入地址：");
        String address = sc.next();

        Student stu = new Student(id, name, age, address);
        list.add(stu);
        System.out.println("添加成功！");
    }
}